import {
  HttpException,
  HttpStatus,
  Injectable,
  NestMiddleware,
} from '@nestjs/common';
const AUTH_KEY = 'sLQbAGfj2ECyTJgn';

@Injectable()
export class ValidateRequestHeaderMiddleware implements NestMiddleware {
  use(req: any, res: any, next: () => void) {
    const authHeader = req.header('X-AUTH-TOKEN');

    if (authHeader === AUTH_KEY) {
      next();
    } else {
      throw new HttpException(
        {
          message: 'Need Auth',
          error: 'Forbiden',
          status: HttpStatus.FORBIDDEN,
        },
        HttpStatus.FORBIDDEN,
      );
    }
  }
}
